import uuid
import random
import os
import django



os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TasteLink0.settings")
django.setup()
from app.services.order_service import OrderService
from datetime import datetime, timedelta
from app.models import (
    User, MerchantInfo, Category, Dish, Image, DishImage
)


# 工具函数：读取本地图片为二进制数据
def read_image_binary(file_path):
    with open(file_path, 'rb') as f:
        return f.read()

#
# # ================== Step 1: 创建新商家 ==================
new_merchant = MerchantInfo.objects.get(merchant_id='422156b8-c6be-41f2-9ddf-1c8b7d5c86e0')
# print("✅ 新商家【川味小馆】已创建")
#
# # ================== Step 2: 创建分类 ==================
# category_hot = Category.objects.create(
#     merchant=new_merchant,
#     category_name="热菜",
#     sort_order=1
# )
# category_cold = Category.objects.create(
#     merchant=new_merchant,
#     category_name="冷饮/小吃",
#     sort_order=2
# )
# print("✅ 分类【热菜】【冷饮/小吃】已创建")
#
# # ================== Step 3: 定义菜品信息和图片路径 ==================
# dish_info_list = [
#     {
#         "name": "宫保鸡丁",
#         "price": 28.00,
#         "stock": 50,
#         "category": category_hot,
#         "image_path": "gongbaojiding.jpg",
#         "customization_options": {"辣度": ["微辣", "中辣", "特辣"], "加料": ["花生", "腰果"]}
#     },
#     {
#         "name": "水煮鱼",
#         "price": 48.00,
#         "stock": 30,
#         "category": category_hot,
#         "image_path": "shuizhuyu.jpg",
#         "customization_options": {"辣度": ["中辣", "特辣"], "配菜": ["豆芽", "白菜"]}
#     },
#     {
#         "name": "柠檬水",
#         "price": 12.00,
#         "stock": 100,
#         "category": category_cold,
#         "image_path": "ningmengshui.jpg",
#         "customization_options": {"糖度": ["无糖", "半糖", "全糖"], "冰块": ["去冰", "少冰", "多冰"]}
#     },
#     {
#         "name": "牛肉面",
#         "price": 22.00,
#         "stock": 60,
#         "category": category_hot,
#         "image_path": "niuroumian.jpg",
#         "customization_options": {"辣度": ["不辣", "微辣"], "面条": ["细面", "宽面"], "加肉": ["加一份牛肉"]}
#     },
#     {
#         "name": "炸薯条",
#         "price": 15.00,
#         "stock": 80,
#         "category": category_cold,
#         "image_path": "zhashutiao.jpg",
#         "customization_options": {"酱料": ["番茄酱", "辣椒酱", "芝士酱"], "份量": ["标准", "加大"]}
#     },
#     {
#         "name": "猪肉饺子",
#         "price": 18.00,
#         "stock": 40,
#         "category": category_hot,
#         "image_path": "zhuroujiaozi.jpeg",
#         "customization_options": {"蘸料": ["醋+蒜", "酱油+芥末"], "数量": ["6个", "12个"]}
#     }
# ]
#
# # ================== Step 4: 创建菜品 + 插入图片 + 关联 ==================
# created_dishes = []

# for idx, info in enumerate(dish_info_list, start=1):
#     dish_id = uuid.uuid4()
#
#     # 插入图片
#     try:
#         image_data = read_image_binary(info["image_path"])
#     except FileNotFoundError as e:
#         print(f"❌ 文件未找到：{e}")
#         continue
#
#     image = Image.objects.create(
#         image_data=image_data,
#         mime_type='image/jpeg'
#     )
#
#     # 创建菜品
#     dish = Dish.objects.create(
#         dish_id=dish_id,
#         merchant=new_merchant,
#         category=info["category"],
#         dish_name=info["name"],
#         price=info["price"],
#         stock=info["stock"],
#         sort_order=idx,
#         status='on_shelf',
#         customization_options=info["customization_options"]
#     )
#     created_dishes.append(dish)
#
#     # 创建图片关联
#     DishImage.objects.create(
#         dish=dish,
#         image=image,
#         sort_order=0
#     )
#
#     print(f"✅ 菜品【{info['name']}】及其图片已创建并关联")
#
# print("🎉 全部菜品及图片插入完成！")


# ================== Step 5: 调用 OrderService 创建订单 ==================
user_id = 'dbea969a-c189-4cf4-afc6-30fef4d291e5'
# 使用你刚才创建的商家 ID
merchant_id = new_merchant.merchant_id

# 从前端传入的 dish_id 列表（你可以手动从 created_dishes 中获取）
items = [
    {"dish_id": "21d8edc8-cb44-4326-8e93-c6ca9b3ec231", "quantity": 2, "customization": {"辣度": "中辣", "加料": "花生"}},
    {"dish_id": "ce5eeeaf-fdd9-4b97-8a95-1723c815c793", "quantity": 1, "customization": {"辣度": "特辣", "配菜": "豆芽"}},
    {"dish_id": "4b9161d1-6686-48aa-900b-bee6897b5521", "quantity": 1, "customization": {"糖度": "半糖", "冰块": "少冰"}}
]

table_number = "A1"

# 调用下单服务
response = OrderService.create_order(
    user_id=user_id,
    merchant_id=merchant_id,
    items=items,
    table_number=table_number
)

print("📦 下单响应：", response)